#include <iostream>
#include <vector>
#include <unordered_map>
#include <algorithm>
#include <numeric>

using namespace std;

// 2841. 几乎唯一子数组的最大和
// https://leetcode.cn/problems/maximum-sum-of-almost-unique-subarray/description/

class Solution
{
public:
    long long maxSum(vector<int> &nums, int m, int k)
    {
        unordered_map<int, int> recordNumber;
        int left = 0;
        long long ans = 0;
        long long sum = 0;
        for (int i = 0; i < nums.size(); i++)
        {
            ++recordNumber[nums[i]];
            sum += nums[i];
            if (i - left + 1 < k)
            {
                continue;
            }
            if (recordNumber.size() >= m)
            {
                ans = max(sum, ans);
            }
            if (recordNumber[nums[left]] == 1)
            {
                recordNumber.erase(nums[left]);
            }
            else
            {
                recordNumber[nums[left]]--;
            }
            sum -= nums[left++];
        }
        return ans;
    }
};

int main()
{
    return 0;
}